package com.btashov.stringarray;

import com.btashov.datastructure.Node;

import java.util.*;

/**
 * @author Bahtiyar Tashov <btashov@gmail.com>
 */
//todo: cover with test left methods
public class ArrayUtils {

    private static final String INVALID_ARRAY = "Invalid array.";

    public static Map<Integer, Node> buildMap(Node[] nodes) {
        Map<Integer, Node> map = new HashMap<Integer, Node>();
        for (Node node : nodes) {
            map.put(node.getId(), node);
        }
        return map;
    }

    public static List<String> merge(String[] words, String[] more) {
        List<String> sentence = new LinkedList<String>();
        Collections.addAll(sentence, words);
        Collections.addAll(sentence, more);
        return sentence;
    }

    public static List<String> mergeManual(String[] words, String[] more) {
        List<String> sentence = new LinkedList<String>();
        for (String s : words) sentence.add(s);
        for (String s : more) sentence.add(s);
        return sentence;
    }

    public static String makeSentence(String[] words) {
        if (words == null) throw new IllegalArgumentException(INVALID_ARRAY);

        for (String str : words) {
            if (str == null) throw new IllegalArgumentException(INVALID_ARRAY);
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < words.length - 1; i++) {
            sb.append(words[i]);
            sb.append(" ");
        }
        sb.append(words[words.length - 1]);
        return sb.toString();
    }
}
